package com.shangke.corejava.ch14;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;
import java.util.Map;

public class ChatRoomServer {
	private Map<String, Socket> clientMap = new Hashtable<String, Socket>();

	public void start(int port) {
		ServerSocket serverSocket = null;

		try {
			serverSocket = new ServerSocket(port);

			while (true) {
				Socket socket = serverSocket.accept();
				String ip = socket.getInetAddress().getHostAddress();
				clientMap.put(ip, socket);

				new Thread(new ChatRoom(socket, clientMap)).start();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new ChatRoomServer().start(8888);
	}
}
